﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Graphics;
using Tesla.Graphics.Implementation;
using Tesla.Xna.Util;
using XFG = Microsoft.Xna.Framework.Graphics;

namespace Tesla.Xna.Graphics.Implementation {
    /// <summary>
    /// Concrete XNA implementation of <see cref="Texture1D"/>.
    /// </summary>
    public sealed class XNATexture1DImplementation : Texture1DImplementation {
        private XFG.GraphicsDevice _graphicsDevice;
        private XFG.Texture2D _texture2D;

        /// <summary>
        /// Actual XNA Texture.
        /// </summary>
        internal XFG.Texture2D XNATexture1D {
            get {
                return _texture2D;
            }
        }

        /// <summary>
        /// Gets the number of mip map levels in the texture resource.
        /// </summary>
        public override int MipCount {
            get {
                return _texture2D.LevelCount;
            }
        }

        public XNATexture1DImplementation(XFG.GraphicsDevice device, int width, bool genMipMaps, SurfaceFormat format) 
            : base(width, format) {
            _graphicsDevice = device;

            _texture2D = new XFG.Texture2D(device, width, 1, genMipMaps, XNAHelper.ToXNASurfaceFormat(format));
        }

        ~XNATexture1DImplementation() {
            Dispose(false);
        }


        public override void GetData<T>(T[] data, int mipLevel, int left, int right, int startIndex, int elementCount) {
            _texture2D.GetData<T>(mipLevel, new Microsoft.Xna.Framework.Rectangle(0 ,0 ,left, right), data, startIndex, elementCount);
        }

        public override void SetData<T>(T[] data, int mipLevel, int left, int right, int startIndex, int elementCount) {
            _texture2D.SetData<T>(mipLevel, new Microsoft.Xna.Framework.Rectangle(0, 0, left, right), data, startIndex, elementCount);
        }

        #region GraphicsResource methods

        protected override void Dispose(bool disposing) {
            if(!IsDisposed) {
                //Dispose of managed resources
                if(disposing) {
                    if(_texture2D != null) {
                        _texture2D.Dispose();
                    }
                }
            }
            base.Dispose(disposing);
        }
        #endregion
    }
}
